package com.ss.ttvideoengine;

import android.content.Context;
import android.net.Uri;
import android.view.Surface;
import android.view.SurfaceHolder;

import com.ss.ttm.player.IMediaPlayerClient;
import com.ss.ttm.player.MediaPlayerClient;
import com.ss.ttm.player.AndroidMediaPlayerClient;
import com.ss.ttm.player.m;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

public class MediaPlayerClientProxy implements IMediaPlayerClient {
    private MediaPlayerClient mMediaPlayerClient;

    public MediaPlayerClientProxy() {

    }
    public static IMediaPlayerClient createMediaPlayerClient(Context context) {
        MediaPlayerClientProxy proxy = new MediaPlayerClientProxy();
        synchronized (OnCompletionListener.class) {
            if (m.a()) {
                try {
                    Class clazz = Class.forName("com.ss.ttm.player.TTPlayerClient");
                    Method method = clazz.getDeclaredMethod("create", IMediaPlayerClient.class, Context.class);
                    method.setAccessible(true);
                    proxy.mMediaPlayerClient = (MediaPlayerClient) method.invoke(clazz, proxy, context);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }

            if (proxy.mMediaPlayerClient == null) {
                proxy.mMediaPlayerClient = AndroidMediaPlayerClient.createAndroidMediaPlayerClient(proxy);
            }
            return proxy;
        }
    }

    public final void release() {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.release();
        }
    }

    public final void setVolume(float leftvolume, float rightVolume) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setVolume(leftvolume, rightVolume);
        }
    }

    public final void seekTo(int arg2) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.seekTo(arg2);
        }
    }

    public final void setIntOption(int arg2, int arg3) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setIntOption(arg2, arg3);
        }
    }

    public final void setDataSource(Context context, Uri uri, Map<String,String> params) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setDataSource(context, uri, params);
        }
    }

    public final void setSurface(Surface surface) {
        if (surface != null && surface.isValid() && this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setSurface(surface);
        }
    }

    public final void setDisplay(SurfaceHolder surfaceHolder) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setDisplay(surfaceHolder);
        }
    }

    public final void setOnBufferingUpdateListener(OnBufferingUpdateListener onBufferingUpdateListener) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setOnBufferingUpdateListener(onBufferingUpdateListener);
        }
    }

    public final void setOnCompletionListener(OnCompletionListener onCompletionListener) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setOnCompletionListener(onCompletionListener);
        }
    }

    public final void setOnErrorListener(OnErrorListener onErrorListener) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setOnErrorListener(onErrorListener);
        }
    }

    public final void setOnInfoListener(OnInfoListener onInfoListener) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setOnInfoListener(onInfoListener);
        }
    }

    public final void setOnPreparedListener(OnPreparedListener onPreparedListener) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setOnPreparedListener(onPreparedListener);
        }
    }

    public final void setOnSeekCompleteListener(OnSeekCompleteListener onSeekCompleteListener) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setOnSeekCompleteListener(onSeekCompleteListener);
        }
    }

    public final void setOnVideoSizeChangedListener(OnVideoSizeChangedListener onVideoSizeChangedListener) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setOnVideoSizeChangedListener(onVideoSizeChangedListener);
        }
    }

    public final void setIsMute(boolean isMute) {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setIsMute(isMute);
        }
    }

    public final String getStringOption(int arg2) {
        return this.mMediaPlayerClient != null ? this.mMediaPlayerClient.getStringOption(arg2) : null;
    }

    public final void start() {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.start();
        }
    }

    public final long getLongOption(int arg4) {
        if (this.mMediaPlayerClient != null) {
            return this.mMediaPlayerClient.getLongOption(arg4, 0L);
        }
        return 0L;
    }

    public final void pause() {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.pause();
        }
    }

    public final void reset() {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.reset();
        }
    }

    public final void stop() {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.stop();
        }
    }

    public final void prepareAsync() {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.prepareAsync();
        }
    }

    public final int getCurrentPosition() {
        return this.mMediaPlayerClient != null ? this.mMediaPlayerClient.getCurrentPosition() : 0;
    }

    public final int getDuration() {
        return this.mMediaPlayerClient != null ? this.mMediaPlayerClient.getDuration() : 0;
    }

    public final void disableLooping() {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setLooping(false);
        }
    }

    public final void setScreenOnWhilePlaying() {
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setScreenOnWhilePlaying(true);
        }
    }

    public final boolean isOsPlayer() {
        return this.mMediaPlayerClient == null || this.mMediaPlayerClient.getType() == 0;
    }

    public final int getType() {
        return this.mMediaPlayerClient != null ? this.mMediaPlayerClient.getType() : 0;
    }
}

